import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:meta/meta.dart';

part 'gs_bluetooth_state.dart';

class GsBluetoothCubit extends Cubit<GsBluetoothState> {
  GsBluetoothCubit() : super(GsBluetoothState().init());
 
  void _initTimer(){
    state.timer = new Timer.periodic(Duration(seconds: 1), (Timer timer) {
     
      
      if(state.count==0){
        timer.cancel();    //倒计时结束取消定时器
        emit(state.clone()..isButtonEnable=true);  //按钮可点击
        emit(state.clone()..count=11); //重置时间
        emit(state.clone()..bottomColor=Colors.black.withOpacity(0.2));//重新发送按钮颜色
        emit(state.clone()..bottomBprderColor=Colors.black.withOpacity(0.2));//重新发送按钮颜色
        emit(state.clone()..bottonBackColor=Colors.black.withOpacity(0.2));
        emit(state.clone()..buttonText = '重新发送');//重置按钮文本
          
      }else{
        emit(state.clone()..buttonText='${state.count}'+'s'); //更新文本内容
        emit(state.clone()..bottomColor=Color(0xffD1E000));//重新发送按钮颜色
        emit(state.clone()..bottomBprderColor=Color(0xffD1E000));//重新发送按钮颜色
        emit(state.clone()..bottonBackColor=Colors.white);

      }
       state.count--;
    });
  }
  void buttonClickListen(){
    if(state.isButtonEnable){   //当按钮可点击时
      state.isButtonEnable=false; //按钮状态标记
      _initTimer();
    
      return null;   //返回null按钮禁止点击
    }else{     //当按钮不可点击时
    //  debugPrint('false');
      return null;    //返回null按钮禁止点击
    }
  }
  
}
